<?
class Header extends Module{
	function __construct($row){					
		Module::Module($row);
		require_once 'forms/header.php';
		
		if(AZNet::$page['name'] == 'zone'){
			$zone_id = (int)Url::get("zone_id");

			if(!$zone_id || ($zone_id && !isset(CGlobal::$allZones[$zone_id]))){
				//Url::access_denied();
				Url::redirect('home');
			}
		}
		
		if(AZNet::$page['name']=='item_detail'){
			$item_id = (int)Url::get('id',0);
			
			if(Item::$id==-1){
				Item::$id = $item_id;
				Item::$item = Item::get_item($item_id);
				
				AZLib::checkItem();
				
				if(Item::$item && Item::$item['display'])
				{
					CGlobal::$curCategory  	= Item::$item['category_id'];
				}
			}
		}
		
		$curCategory=false;
		
		if(CGlobal::$curCategory && isset(CGlobal::$allCategories[CGlobal::$curCategory]))
		{
			$curCategory = CGlobal::$allCategories[CGlobal::$curCategory];	
		}
		
		$str_keywords 	= '';
		$str_catSub 	= '';		
		$meta_desc 		= '';
		
		if($curCategory)
		{
			if($curCategory['parent_id'] && isset(CGlobal::$allCategories[$curCategory['parent_id']]))//Cấp 2 hoặc cấp 3
			{
				$parent_cat = CGlobal::$allCategories[$curCategory['parent_id']];
				
				CGlobal::$curMainCat	=	CGlobal::$allCategories[$curCategory['parent_id']];
				CGlobal::$curLevel2Cat	=	$curCategory;
			}
			else {				
				CGlobal::$curMainCat=$curCategory;
			}
		}
		
		if(CGlobal::$curMainCat && isset(CGlobal::$curMainCat['zones'])){
			foreach (CGlobal::$curMainCat['zones'] as $zid){
				if($zid && isset(CGlobal::$allZones[$zid])){
					
					CGlobal::$curZone = CGlobal::$allZones[$zid];
					
					if(CGlobal::$curMainCat['id'] != 100){
						break;
					}
				}
			}
		}
		
		if(!CGlobal::$curZone){
			/*if(AZNet::$page['name'] == 'post_item'){
				$zid = (int)Url::get("zid",0);
			}
			else*/
			{
				$zid = (int)Url::get("zone_id",0);
			}
			
			if($zid && isset(CGlobal::$allZones[$zid])){
				CGlobal::$curZone = CGlobal::$allZones[$zid];
			}
		}
		
		
		if(AZNet::$page['name']=='zone')//Trang zone
		{
			if(REWRITE_ON){//tuannk add 2010.07.28 fix redirect 301 với các url link sai
				$ebname = CGlobal::$curZone['ebname'];
	
				$eburl 	= WEB_DIR."z".CGlobal::$curZone['id']."/$ebname.html";
				
				if($ebname!='' && strpos($_SERVER['REQUEST_URI'],$eburl) !==0 ){
					Url::redirect_url($eburl,301);
				}
			}
			
			##########################################
			#For SEO
			if(isset(CGlobal::$curZone['cats'])){
				foreach (CGlobal::$curZone['cats'] as $catid){
					if(isset(CGlobal::$allCategories[$catid])){
						$str_catSub .= ($str_catSub?', ':'').CGlobal::$allCategories[$catid]['name'];
						
						if(CGlobal::$allCategories[$catid]['keywords']){
							$str_keywords .= ($str_keywords?', ':'').CGlobal::$allCategories[$catid]['keywords'];
						}
					
						if(CGlobal::$allCategories[$catid]['description']){
							$meta_desc .= ($meta_desc?', ':'').CGlobal::$allCategories[$catid]['description'];
						}
					}
				}
			}
			
			
			if(!$str_keywords){
				if($str_catSub){
					$str_keywords = $str_catSub;
				}
				else{
					$str_keywords = CGlobal::$curZone['name'];
				}
			}
			if(!$meta_desc){
				if($str_catSub){
					$meta_desc = $str_catSub;
				}
				else{
					$meta_desc = CGlobal::$curZone['name'];
				}
			}
			
			CGlobal::$keywords = $str_keywords;
			CGlobal::$meta_desc = $meta_desc;
			
			CGlobal::$website_title = CGlobal::$curZone['name'];
			
			#For SEO
			##########################################
		}
		else{
			$catid 		= CGlobal::$curCategory;
			
			if(isset(CGlobal::$allCategories[$catid]) && CGlobal::$allCategories[$catid]['ref_id']){//redirected
				$ref_id = CGlobal::$allCategories[$catid]['ref_id'];
				
				$ebname = '';
				if(isset(CGlobal::$allCategories[$ref_id])){
					$ebname = CGlobal::$allCategories[$ref_id]['ebname'];
					
					if(CGlobal::$allCategories[$ref_id]['parent_id']){//DM cấp 2 hoặc 3
						$pid  = CGlobal::$allCategories[$ref_id]['parent_id'];
						
						if(isset(CGlobal::$allCategories[$pid])){
							$pcat = CGlobal::$allCategories[$pid];
							
							$ebname = "{$pcat['ebname']}-".CGlobal::$allCategories[$ref_id]['ebname'];
							
							if(CGlobal::$allCategories[$pid]['parent_id']){//DM cấp 3
								$ppid  = CGlobal::$allCategories[$pid]['parent_id'];
							
								if(isset(CGlobal::$allCategories[$ppid])){
									$ppcat = CGlobal::$allCategories[$ppid];
						
									$ebname = "{$ppcat['ebname']}-{$pcat['ebname']}-".CGlobal::$allCategories[$ref_id]['ebname'];
								}
							}
						}
					}
					
					Url::redirect_url(WEB_DIR."c$ref_id/$ebname",301);
				}
			}
			
			if(AZNet::$page['name']=='list_detail' && REWRITE_ON){//tuannk add 2010.07.28 fix redirect 301 với các url link sai
				$ebname_req = Url::get("ebname");
				$page_no 	= (int)Url::get("page_no",1);
				if($page_no <= 1){
					$page_no = 0;
				}
				
				$ebname 	= '';
				$eburl 		= '';
				
				if(CGlobal::$curMainCat){
					$ebname = CGlobal::$curMainCat['ebname'];
					
					if(CGlobal::$curLevel2Cat){
						$ebname .= "-".CGlobal::$curLevel2Cat['ebname'];
						
						$eburl = WEB_DIR."c".CGlobal::$curLevel2Cat['id']."/".$ebname;
					}
					else{
						$eburl = WEB_DIR."c".CGlobal::$curMainCat['id']."/".$ebname;
					}
				}
				
				
				if($ebname!='')
				{
					if($page_no)
					{
						$eburl .= "/page-$page_no";
					}
					
					if(strpos($_SERVER['REQUEST_URI'],$eburl) !==0 || $ebname_req!=$ebname || (isset($_GET['page_no']) && $page_no <=1)){
						Url::redirect_url($eburl,301);
					}
				}
			}
			
			if(CGlobal::$curLevel2Cat){//Nếu có cả 2 cấp
				$str_catSub = CGlobal::$curLevel2Cat['name'].', '.CGlobal::$curMainCat['name'];
				
				//Cho subcat
				if(CGlobal::$curLevel2Cat['keywords']){
					CGlobal::$keywords = CGlobal::$curLevel2Cat['keywords'];
				}
				
				if(CGlobal::$curLevel2Cat['description']){
					$meta_desc = CGlobal::$curLevel2Cat['description'];
				}
				//End Cho subcat	
				
				if(CGlobal::$curMainCat['description']){
					$meta_desc .= ($meta_desc?', ':'').CGlobal::$curMainCat['description'];
				}
				//End Cho maincat
			}
			elseif(CGlobal::$curMainCat){//Nếu chỉ có 1 cấp
				if(isset(CGlobal::$allCategories[CGlobal::$curMainCat['id']]) && isset(CGlobal::$subCategories[CGlobal::$curMainCat['id']])){
					$subCats=CGlobal::$subCategories[CGlobal::$curMainCat['id']];
					
					foreach ($subCats as $catSub){
						if($catSub['name']){
							$str_catSub .= ($str_catSub?', ':'').$catSub['name'];
						}									
						
						if($catSub['description']){
							$meta_desc .= ($meta_desc?', ':'').$catSub['description'];
						}	
					}
				}	
				
				$str_catSub .= ($str_catSub?', ':'').CGlobal::$curMainCat['name'];			
				
				if(CGlobal::$curMainCat['keywords']){
					CGlobal::$keywords = CGlobal::$curMainCat['keywords'];
				}
			
				if(CGlobal::$curMainCat['description']){
					$meta_desc .= ($meta_desc?', ':'').CGlobal::$curMainCat['description'];
				}
			}							
	
			//CGlobal::$curCategory
			
			if(AZNet::$page['name']=='item_detail'){
				if(Item::$item && Item::$item['status']==1)
				{
				 	CGlobal::$meta_desc = AZLib::word_limit(AZLib::descriptionText(Item::$item['brief'] . ' ' . Item::$item['description']),30,'');
				}
				elseif($meta_desc){				
					CGlobal::$meta_desc = $meta_desc;
				}
			}
			elseif($meta_desc){				
				CGlobal::$meta_desc = $meta_desc;
			}
		}
		
		//limit character description and keyword
		CGlobal::$keywords 	= AZLib::word_limit(CGlobal::$keywords,20,'');
		CGlobal::$meta_desc = AZLib::word_limit(CGlobal::$meta_desc,250,'');
			
		//Gán title cho các trang trong! 
		$page_title_arr=array();
		$page_title_arr['post_item']="Đăng sản phẩm";
		$page_title_arr['sign_in']  ="Đăng nhập - Đăng ký thành viên";
		
		if(isset($page_title_arr[AZNet::$page['name']])){
			CGlobal::$website_title = $page_title_arr[AZNet::$page['name']].' | '.CGlobal::$website_title;
		}
				
		$this->add_form(new HeaderForm);
	}
}
?>